package com.food.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.food.dao.in.FoodsaveDAO;
import com.food.model.Foodsave;

public class FoodsaveDAOJdbc implements FoodsaveDAO {
//	private static final String URL = "jdbc:sqlserver://192.168.34.108:1433;database=PJDB";
//	private static final String USER = "sa";
//	private static final String PASSWORD = "sa123456";
	
	private DataSource ds = null;
	public FoodsaveDAOJdbc() {
		try {
			Context ctx = new InitialContext();
			this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/food");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	
	private static final String INSERT ="insert into foodsave (rid, fsid) values(?,?)";
	@Override
	public void insertFoodsave(Foodsave bean) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(INSERT);
			pstmt.setInt(1, bean.getRid());
			pstmt.setInt(2, bean.getFsid());
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
	private static final String UPDATE ="update foodsave set rid=?, fsid=? where foodsaveid =?";
	@Override
	public void updateFoodsave(int rid, int fsid, int foodsaveid) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(UPDATE);
			pstmt.setInt(1, rid);
			pstmt.setInt(2, fsid);
			pstmt.setInt(3, foodsaveid);
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
	private static final String DELETE ="delete from foodsave where foodsaveid =?";
	@Override
	public void deleteFoodsave(int foodsaveid) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(DELETE);
			pstmt.setInt(1, foodsaveid);
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}


	}
	private static final String SELECTByRecipe ="select foodsaveid ,rid,fsid from foodsave where rid=?";
	@Override
	public List<Foodsave> findFoodsaveByRecipe(int rid) {
		List<Foodsave> result = null;
		Foodsave bean = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(SELECTByRecipe);
			pstmt.setInt(1, rid);
			rs = pstmt.executeQuery();
			result = new ArrayList<Foodsave>();
			while(rs.next()){
				bean = new Foodsave();
				bean.setFoodsaveid(rs.getInt("foodsaveid"));
				bean.setRid(rs.getInt("rid"));
				bean.setFsid(rs.getInt("fsid"));
				result.add(bean);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	private static final String SELECTByFoodsearch ="select foodsaveid ,rid,fsid from foodsave where fsid=?";
	@Override
	public List<Foodsave> findFoodsaveByFoodsearch(int fsid) {
		List<Foodsave> result = null;
		Foodsave bean = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(SELECTByFoodsearch);
			pstmt.setInt(1, fsid);
			rs = pstmt.executeQuery();
			result = new ArrayList<Foodsave>();
			while(rs.next()){
				bean = new Foodsave();
				bean.setFoodsaveid(rs.getInt("foodsaveid"));
				bean.setRid(rs.getInt("rid"));
				bean.setFsid(rs.getInt("fsid"));
				result.add(bean);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	public static void main(String[] args){
//		FoodsaveDAOJdbc dao = new FoodsaveDAOJdbc();
		
	}
}
